#include "drawwidget.h"

DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
    setAutoFillBackground(true);
    setPalette(QPalette(Qt::white));
    pix=new QPixmap(size());
    pix->fill(Qt::white);

    //设置绘制区窗口大小
    setMinimumSize(600,400);

}

void DrawWidget::getColor1(QColor color1)
{
    color=color1;

}

void DrawWidget::getfontType(int fontType)
{
    fontType1=fontType;

}

void DrawWidget::getFontWidth(int fontw)
{
    fontw1=fontw;

}

void DrawWidget::ClearAll()
{
    QPixmap *cPix=new QPixmap(size());
    cPix->fill(Qt::white);
    pix=cPix;
    update();

}

void DrawWidget::resizeEvent(QResizeEvent *event)
{


    if(height()>pix->height()||width()>pix->width())
    {
        QPixmap *pixmap=new QPixmap(size());
        QPainter painter(pixmap);
        pixmap->fill(Qt::white);
        painter.drawPixmap(QPoint(0,0),*pix);
        pix=pixmap;

    }
    QWidget::resizeEvent(event);

}

void DrawWidget::mouseMoveEvent(QMouseEvent *event)
{
    QPainter painter;
    QPen pen;

    pen.setStyle((Qt::PenStyle)fontType1);

    qDebug()<<fontType1;
    pen.setWidth(fontw1);
    pen.setColor(color) ;

    painter.begin(pix);
    painter.setPen(pen);
    painter.drawLine(startpos,event->pos());
    painter.end();

    startpos=event->pos();
    update();
}

void DrawWidget::mousePressEvent(QMouseEvent *event)
{
    startpos=event->pos();
}


void DrawWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(QPoint(0,0),*pix);
}

